<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>241123-chat-demo</title>

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        html, body {
            height: 100%;
            background-color: #f5f5f5;
            font-family: 'Arial', sans-serif;
        }

        .chat-container {
            height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            position: relative;
        }

        .chat-history {
            border: 1px solid #ddd;
            height: 300px;
            flex-grow: 1;
            padding: 16px;
            overflow-y: auto;
            background-color: white;
            margin-top: 60px;
        }

        .input-container {
            border-top: 1px solid #ddd;
            padding: 16px;
            display: flex;
            justify-content: space-between;
            background-color: white;
        }

        #message-input {
            flex-grow: 1;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 25px;
            font-size: 16px;
        }

        #send-button {
            margin-left: 8px;
            padding: 10px 20px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 25px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s, transform 0.2s;
        }

        #send-button:hover {
            background-color: #0056b3;
            transform: translateY(-2px);
        }

        #send-button:active {
            transform: translateY(0);
        }

        .message {
            margin-bottom: 15px;
            display: flex;
        }

        .message-content {
            padding: 10px 15px;
            border-radius: 15px;
            max-width: 70%;
        }

        .user-message {
            justify-content: flex-end;
        }

        .ai-message {
            justify-content: flex-start;
        }

        .user-message .message-content {
            background: #007bff;
            color: white;
        }

        .ai-message .message-content {
            background: #e9ecef;
            color: black;
        }

        .quick-inputs {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-evenly;
            gap: 15px;
            margin-top: 20px;
            padding: 10px;
        }

        .quick-input-btn {
            padding: 10px 20px;
            background-color: #4caf50;
            color: white;
            border: none;
            border-radius: 30px;
            font-size: 14px;
            cursor: pointer;
            transition: background-color 0.3s, transform 0.2s;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        .quick-input-btn:hover {
            background-color: #45a049;
            transform: translateY(-2px);
        }

        .quick-input-btn:active {
            transform: translateY(0);
        }

        .return-home-btn {
            position: fixed;
            top: 10px;
            right: 10px;
            z-index: 1000;
            padding: 10px 20px;
            font-size: 14px;
            background-color: #6c757d;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        .return-home-btn:hover {
            background-color: #5a6268;
        }

        @media (max-width: 768px) {
            .quick-inputs {
                justify-content: center;
            }

            .quick-input-btn {
                width: 100%;
                max-width: 220px;
            }
        }
    </style>
</head>
<body>

<div class="chat-container">

    <!-- 返回首页按钮 -->
    <a href="/" class="return-home-btn">返回首页</a>

    <div class="chat-history" id="chat-history">
        <!-- 聊天记录将显示在这里 -->
    </div>

    <div class="quick-inputs">
        <button class="quick-input-btn" onclick="quickSend('告诉我一个笑话！')">讲个笑话</button>
        <button class="quick-input-btn" onclick="quickSend('今天天气怎么样？')">问天气</button>
        <button class="quick-input-btn" onclick="quickSend('你是谁？')">问AI</button>
        <button class="quick-input-btn" onclick="quickSend('你能做什么？')">功能介绍</button>
    </div>

    <div class="input-container">
        <input type="text" id="message-input" placeholder="输入消息...">
        <button id="send-button">发送</button>
    </div>
</div>

<script src="jquery.min.js"></script>

<script>
    const chatHistory = document.getElementById('chat-history');
    const messageInput = document.getElementById('message-input');
    const sendButton = document.getElementById('send-button');
    sendButton.addEventListener('click', send);

    // 回车发送
    messageInput.addEventListener('keypress', (e) => {
        if (e.key === 'Enter') {
            send();
        }
    });

    function send() {
        const message = messageInput.value.trim(); // 去除前后空格
        if (message) {
            addMessage(message);
            messageInput.value = '';
            getResponseFromServer(message);
        }
    }

    function getResponseFromServer(message) {
        $.get("/ai/chat", {message: message}, function (resp) {
            addMessage(resp, true);
        });
    }

    function addMessage(content, isAI = false) {
        const messageDiv = document.createElement('div');
        messageDiv.className = `message ${isAI ? 'ai-message' : 'user-message'}`;

        const messageContent = document.createElement('div');
        messageContent.className = 'message-content';
        messageContent.textContent = content;

        messageDiv.appendChild(messageContent);
        chatHistory.appendChild(messageDiv);

        // 滚动到最新消息
        chatHistory.scrollTop = chatHistory.scrollHeight;
    }

    function quickSend(text) {
        messageInput.value = text;
        send();
    }
</script>

</body>
</html>
